<template>
  <div id="login">
    <el-row type="flex" justify="center">
      <el-col :span="8">
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" class="demo-ruleForm"
                 label-position="left" label-width="0px" v-loading="loading" align="center">
          <h3 class="login_title">系统登录</h3>
          <el-form-item prop="username"  class="in">
            <el-input v-model="ruleForm.username" placeholder="用户名"></el-input>
          </el-form-item>
          <el-form-item prop="password" class="in">
            <el-input type="password" @keyup.native.enter="submitForm('ruleForm')" v-model="ruleForm.password" autocomplete="off" placeholder="密码"></el-input>
          </el-form-item>
<!--          <el-form-item id="choose" prop="roleId" style="margin-bottom: 2px">-->
<!--            <span style="left:10px;padding-left: 10px; padding-right: 70px; font-weight: bolder">角色</span>-->
<!--            <template>-->
<!--              <el-radio v-model="ruleForm.roleId" label="1" style="color: #8c939d;font-family: '微软雅黑';font-weight: bolder">管理员</el-radio>-->
<!--              <el-radio v-model="ruleForm.roleId" label="2" style="color: #8c939d;font-family: '微软雅黑';font-weight: bolder">员工</el-radio>-->
<!--              <el-radio v-model="ruleForm.roleId" label="3" style="color: #8c939d;font-family: '微软雅黑';font-weight: bolder">用户</el-radio>-->
<!--            </template>-->
<!--          </el-form-item>-->
          <el-form-item>
            <el-button type="primary" style="margin-bottom: 10px;width: 80%;border-radius: 20px; border: none" v-on:click="submitForm('ruleForm')">登录</el-button>
          </el-form-item>
          <el-form-item style="">
            <el-link id="cc"  type="primary" :underline="false"  href="/reg">注册用户</el-link>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<style>
  #cc{
    bottom: -10px;
    left: 10px;
    position: absolute;
    display: flex;
    font-weight: bold;
  }
  #choose{
    display: flex;
    color: pink;
  }
  .in input.el-input__inner{
    border-radius: 20px;
  }
  #login {
    background: url(../img/eva2.jpg) no-repeat;
    background-position: center;
    top: 0;
    left: 0;
    height: 100%;
    width: 100%;
    background-size: 100% 100%;
    position: fixed;
    font-family: Avenir, Helvetica, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    color: #2c3e50;
  }
  .login_title {
    /*margin: 0 auto 40px auto;*/
    /*position: center;*/
    text-align: center;
    color: pink;
  }
  .demo-ruleForm{
    border-radius: 15px;
    background-clip: padding-box;
    margin: 90px auto;
    width: 350px;
    height: auto;
    padding: 35px 35px 15px 35px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
  }

  nav {
    padding: 30px;
  }

  nav a {
    font-weight: bold;
    color: #2c3e50;
  }

  nav a.router-link-exact-active {
    color: #42b983;
  }

</style>
<script>
  export default {
    data() {
      return {
        ruleForm: {
          password: '',
          username: '',
        },
        rules: {
          username: [
            { required: true, message: '请输入用户名称', trigger: 'blur' },
            { min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
          ],
          // roleId: [
          //   { required: true, message: '请选择身份', trigger: 'blur'},
          // ],
        },
        checked: true,
        loading: false,
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let url = 'http://176.17.10.223:18888/user/login';
            let data = 'username=' + this.ruleForm.username +
                       '&password=' + this.ruleForm.password;
            // 这种方式需要后端用到 @RequestBody 注解
            /*let data = {
              "password": this.ruleForm.password,
              "username": this.ruleForm.username
            }*/
            this.axios.post(url, data).then((response) => {
              let state = response.data.state;
              let message = response.data.message;
              if (state == 20000){
              // 将服务器返回的jwt给存起来
              localStorage.setItem('JWT_TOKEN',response.data.data)
                this.$message({
                  // 返回成功的数据
                  message: '登录成功',
                  type: 'success'
                });
                this.$router.push({path: '/home'});  //页面路由刷新
              }else {
                console.log(message);
                this.$message.error(message);
              }
            })
                    .catch(function (error) {
                      console.log(error);
                    //  hgjkashgkja
                    });
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>
